import Vue from "vue";
import iView from "view-design";
import VueRouter from "vue-router";
import Routers from "./router";
import Util from "./libs/util";

import App from "./App.vue";
import "view-design/dist/styles/iview.css";
import pageTable from "./views/components/pageTable.vue";


import store from "./store/store";

Vue.use(VueRouter);
Vue.use(iView);
Vue.config.silent = true;
Vue.prototype.util = Util;
Vue.component("pageTable", pageTable);

Vue.prototype.sendMessage = function (data) {
  Socket.send(data);
};

const routerPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(path) {
  return routerPush.call(this, path).catch((err) => err);
};
// 路由配置
const RouterConfig = {
  routes: Routers,
};
const router = new VueRouter(RouterConfig);

const listenSocket = () => {
  let socketState = store.state.socketState;
  let token = sessionStorage.getItem("Token");
  if (token && socketState == "Disconnected") {
    // Socket.(token, store);
    // Socket.onMessage((res) => {
    //   store.dispatch("getSocketDataAction", res);
    // });
    // Socket.onOpen((res) => {
    //   store.commit("websocket_state", "Connected");
    // });
    // Socket.onError((res) => {
    //   store.commit("websocket_state", "Disconnected");
    // });
    // Socket.onClose((res) => {
    //   store.commit("websocket_state", "Disconnected");
    // });
  }
};
router.beforeEach((to, from, next) => {
  iView.LoadingBar.start();
  Util.setTitle(to.meta.title);
  // if (to.path != "/" && to.path != "/info") {
  //   listenSocket();
  // }
  if (to.path == "/machineList") {
    listenSocket();
  }
  next();
});

router.afterEach((to, from, next) => {
  iView.LoadingBar.finish();
  window.scrollTo(0, 0);
});

new Vue({
  router,
  store,
  data: {
    // Socket,
  },
  render: (h) => h(App),
}).$mount("#app");